<?php
declare (strict_types=1);

namespace app\index\controller;

use app\common\model\User;
use app\common\tool\Result;
use think\facade\View;
use think\exception\ValidateException;
use think\Response;

class Security extends Base
{
    public function index(): string
    {
        return View::fetch();
    }

    public function edit($type): string
    {
        $title = $type == 'pay_password' ? '修改支付密码' : '修改登录密码';
        View::assign('title', $title);

        return View::fetch();
    }

    public function save(): Response
    {
        $param = $this->request->post();
        try {
            $this->validate($param, 'Security');
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }

        return Result::fastReturn(User::security($param, $this->user));
    }
}